package com.sls.web.manager.action.recharge;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.json.annotations.JSON;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.SystemProduct;
import com.sls.web.manager.recharge.querybean.SystemProductQuery;
import com.sls.web.manager.recharge.service.SystemProductService;

/**
 * 通用价格管理<br>
 * currency_price_manage
 * 
 * @author sxf
 * 
 */
public class CurrencyPriceManageAction extends SuperAction {
	private static final long serialVersionUID = -8440359834431149696L;

	@Resource(name = "systemProductService")
	private SystemProductService systemProductService;

	// 页面显示使用
	private Pagination<SystemProduct> page;
	private SystemProductQuery systemProductQuery;
	private String cardName;// 产品名称
	private String productType;// 产品类型

	/* 用于存放JSON生成后的字符串结果 */
	private String jsonResult;

	public String list() throws Exception {
		if (systemProductQuery == null) {
			systemProductQuery = new SystemProductQuery();
			systemProductQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			systemProductQuery.setPageSize(Constants.INIT_PAGESIZE);
			systemProductQuery.setProductType(1);
		}
		if (StringUtils.isNotBlank(cardName)) {
			systemProductQuery.setName(cardName);
		}
		if (StringUtils.isNotBlank(productType)) {
			try {
				systemProductQuery.setProductType(Integer.valueOf(productType));
			} catch (Exception e) {

			}
		}
		page = systemProductService.queryPage(systemProductQuery,
				getCurrentOperator());
		return "list";
	}

	public String doEdit() throws Exception {
		String id = WebTools.getParameter("id");
		String channelsPrice = WebTools.getParameter("channelsPrice");
		String doorPrice = WebTools.getParameter("doorPrice");
		String doorAddPrice = WebTools.getParameter("doorAddPrice");
		if (StringUtils.isNotBlank(id)) {
			SystemProduct product = new SystemProduct();
			product.setId(id);
			try {
				product.setChannelAgentPrice((int) (Double
						.parseDouble(channelsPrice) * 1000));
			} catch (Exception e) {

			}
			try {
				product.setDoorPrice((int) (Double.parseDouble(doorPrice) * 1000));
			} catch (Exception e) {

			}
			try {
				product.setDoorAddPrice((int) (Double.parseDouble(doorAddPrice) * 1000));
			} catch (Exception e) {

			}
			try {
				systemProductService.update(product, getCurrentOperator());
				jsonResult = "success";
			} catch (Exception e) {
				jsonResult = "error";
			}
		}

		return "json";
	}

	public Pagination<SystemProduct> getPage() {
		return page;
	}

	public void setPage(Pagination<SystemProduct> page) {
		this.page = page;
	}

	public String getCardName() {
		return cardName;
	}

	public void setCardName(String cardName) {
		this.cardName = cardName;
	}

	public SystemProductQuery getSystemProductQuery() {
		return systemProductQuery;
	}

	public void setSystemProductQuery(SystemProductQuery systemProductQuery) {
		this.systemProductQuery = systemProductQuery;
	}

	public String getProductType() {
		return productType;
	}

	public void setProductType(String productType) {
		this.productType = productType;
	}

	@JSON(name = "JSONRESULT")
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
}
